//
//  BottomCustomAlert.m
//  Exchange
//
//  Created by 张庆勇 on 2019/5/7.
//  Copyright © 2019年 张庆勇. All rights reserved.
//

#import "BottomCustomAlert.h"
@implementation BottomCustomAlert
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [UIView animateWithDuration:0.325 animations:^{
        
        self.frame = CGRectMake(0,  UIScreenHeight - self.height, self.width, self.height);
    }];
}

- (instancetype)initWithCustomContentDataArray:(NSArray *)dataArray
{
    if (self = [super init]) {
        _dataArray = dataArray;
        CGFloat height;
        if (dataArray.count>9) {
            height = 10 *45 +IPhoneBottom;
        }else
        {
            height = (dataArray.count+1) *45 +IPhoneBottom;
        }
        self.frame = CGRectMake(0, UIScreenHeight - height, UIScreenWidth, height);
        self.backgroundColor = WHITECOLOR;
        [self addSubview:self.tableview];
        [self addSubview:self.cancelView];
        [_cancelView addSubview:self.cancelbutton];
    }
    return self;
}
- (UIView *)cancelView
{
    if (!_cancelView) {
        _cancelView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 45 -IPhoneBottom, UIScreenWidth, 45)];
    }
    return _cancelView;
}
- (UIButton *)cancelbutton
{
    WeakSelf
    if (!_cancelbutton) {
        _cancelbutton = [UIButton dd_buttonCustomButtonWithFrame:CGRectMake(0, 0, UIScreenWidth, 45) title:@"取消" backgroundColor:WHITECOLOR titleColor:MAINCOLOR tapAction:^(UIButton *button) {
            if (weakSelf.cancelBlock) {
                weakSelf.cancelBlock();
            }
        }];
        _cancelbutton.titleLabel.font = AutoBoldFont(14);
        _cancelbutton.centerX = _cancelView.centerX;
    }
    return _cancelbutton;
}
- (UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, 45 *_dataArray.count)];
        _tableview.tableFooterView = [UIView new];
        _tableview.separatorColor = RGBA(221, 221, 221, 1);
        _tableview.backgroundColor = WHITECOLOR;
        _tableview.delegate = self;
        _tableview.dataSource=self;
        _tableview.showsVerticalScrollIndicator = NO;
        [_tableview registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
    }
    return _tableview;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"CustomCell"];

    cell.title.text = _dataArray[indexPath.row];
    cell.title.font = AutoFont(14);
    if (indexPath.row ==_index) {
         cell.title.textColor = MAINCOLOR;
    }else
    {
         cell.title.textColor = MAINTITLECOLOR;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectIndexPathRow) {
        self.selectIndexPathRow(indexPath.row);
    }
}

@end

@implementation CustomCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = WHITECOLOR;
        self.selectionStyle = UITableViewCellAccessoryNone;
        [self get_up];
        [self setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        
    }
    return self;
}
- (void)get_up
{
    [self.contentView addSubview:self.title];
    
}
-(UILabel *)title
{
    if (!_title) {
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0,0, UIScreenWidth, 44)];
        _title.font = AutoFont(16);
        _title.textAlignment = NSTextAlignmentCenter;
    }
    return _title;
}
@end
